﻿
Imports std
Imports storoomc
Imports wfileis.data

Public Module wfileisclear
    Private Class arguments
        Public Const host As String = "host"
        Public Const port As String = "port"
    End Class

    Private Class record
        Inherits wfileis.data.record

        Protected Overrides Function enableModify() As Boolean
            Return True
        End Function
    End Class

    Private Class category
        Inherits wfileis.data.category

        Protected Overrides Function enableModify() As Boolean
            Return True
        End Function
    End Class

    Private host As String = Nothing
    Private port As Int32 = 0

    Private Sub listWork(Of T As list)(ByVal i As T, ByVal key As String, ByVal subprefix As String)
        assert(Not i Is Nothing)
        If Not i.read(key) Then
            raiseError(errorHandle.errorType.exclamation,
                       "cannot read ", GetType(T).Name, " ", key, " from ", displayHostPort(host, port), ", ignore.")
        Else
            Dim modified As Boolean = False
            Dim it As record.iterator = Nothing
            it = i.begin()
            While it <> i.end()
                Dim subkey As String = Nothing
                subkey = storehelper.generatekey(subprefix, +it)
                If Not storoom.seek(subkey) Then
                    If isdebugbuild() Then
                        raiseError("cannot find subkey ", subkey, ", remove.")
                        pause()
                    End If
                    it = i.erase(it)
                    modified = True
                Else
                    it += 1
                End If
            End While

            If modified Then
                If i.size() > 0 Then
                    If isdebugbuild() Then
                        raiseError(GetType(T).Name, " ", key, " changed, modify as ", Convert.ToString(i))
                        pause()
                    End If
                    i.modify(key)
                Else
                    If isdebugbuild() Then
                        raiseError(GetType(T).Name, " ", key, " has no subkey left, delete")
                        pause()
                    End If
                    If Not storoom.delete(storehelper.generatekey(i, key)) Then
                        raiseError(errorHandle.errorType.exclamation, "cannot delete empty record ", key)
                    End If
                End If
            End If
        End If
    End Sub

    Private Sub recordWork(ByVal key As String)
        listWork(New record(), key, info._keyprefix)
    End Sub

    Private Sub categoryWork(ByVal key As String)
        listWork(New category(), key, record._keyprefix)
    End Sub

    Private Sub work()
        While Not consoleExtSignal()
            Dim s As [set](Of String) = Nothing
            If Not storoom.list(s) Then
                raiseError(errorHandle.errorType.exclamation,
                           "cannot list from ", displayHostPort(host, port), ", wait for next round.")
            Else
                Dim it As [set](Of String).iterator = Nothing
                it = s.begin()
                While it <> s.end() AndAlso Not consoleExtSignal()
                    Dim prefix As String = Nothing
                    Dim key As String = Nothing
                    storehelper.breakupkey((+it), prefix, key)
                    If strsame(prefix, record._keyprefix) Then
                        If isdebugbuild() Then
                            raiseError("working on record ", prefix, " ", key)
                        End If
                        recordWork(key)
                    ElseIf strsame(prefix, category._keyprefix) Then
                        If isdebugbuild() Then
                            raiseError("working on category ", prefix, " ", key)
                        End If
                        categoryWork(key)
                    End If
                    it += 1
                End While

                raiseError("done a round.")
            End If
        End While
    End Sub

    Public Sub main(ByVal iargs() As String)
        startmain()
        Dim args As map(Of String, vector(Of String)) = Nothing
        argmgr.parse(iargs, args)
        If argmgr.getfirstvalue(args, arguments.host, host) AndAlso
           argmgr.getfirstvalue(args, arguments.port, port) Then
            storoom.initial(host, port)
            work()
        Else
            raiseError(errorHandle.errorType.user,
                       applicationName(), " -", arguments.host, " <host> -", arguments.port, " <port>")
        End If
        finishmain()
    End Sub
End Module
